$(function() {
	$("[data-toggle='popover']").popover();
	$('#username').on('blur',function(event) {
		var $self = $(this)
		$.post('./CheckData',{username: $self.val()},function(data){
			if(data.ok === 1){
				$self.closest('.form-group').addClass(' has-success has-feedback');
				$self.after('<span class="glyphicon glyphicon-ok form-control-feedback" aria-hidden="true"></span>');
				$self.popover('hide');
			}else if(data.ok === 0){
				$self.closest('.form-group').addClass(' has-error has-feedback');
				$self.after('<span class="glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>');
				$self.popover('show');
			}
		})
	})
	
	$('#username').on('focus',function(event){
		var $self = $(this)
		$.post('./CheckData',{username: $self.val()},function(data){
			if(data.ok === 1){
				$self.closest('.form-group').removeClass('has-success has-feedback');
	        	$self.next('span').remove();
	        	$self.popover('hide');
			}else if(data.ok === 0){
				$self.closest('.form-group').removeClass('has-error has-feedback');
	        	$self.next('span').remove();
	        	$self.popover('hide');
			}
		})
	})
	
	var vcodeStatus = getQueryVariable("status");
	if(vcodeStatus === "vcodeerror") {
		$('#vcode').popover('show');
		$('#vcode').click( function() {
			$('#vcode').popover('hide');
		})
	}
	
	
})

function changeImg() {
    var vcode = document.getElementById("vcode_img");
    var timestamp = (new Date()).valueOf();
    vcode.src = "./VCode?timestamp=" + timestamp;
}

function getQueryVariable(variable)
{
   var query = window.location.search.substring(1);
   var vars = query.split("&");
   for (var i=0;i<vars.length;i++) {
           var pair = vars[i].split("=");
           if(pair[0] == variable){return pair[1];}
   }
   return(false);
}